package UI;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ChatPanel extends JPanel {
    private String currentUser;
    private static JTextArea messageArea;
    private JTextField inputField;
    private JButton sendButton;
    private  Socket socket;



    public ChatPanel(String currentUser, Socket socket) {
        this.currentUser = currentUser;
        this.socket=socket;
        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(10, 10, 10, 10));

        initUI();
    }

    public static void setMsgToWin(String msg) {
        //更新群聊信息到界面
        messageArea.append(msg);

    }

    private void initUI() {
        // 聊天标题
        JLabel chatTitle = new JLabel("选择联系人开始聊天", JLabel.CENTER);
        chatTitle.setFont(new Font("微软雅黑", Font.BOLD, 16));
        chatTitle.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        add(chatTitle, BorderLayout.NORTH);

        // 消息显示区域
        messageArea = new JTextArea();
        messageArea.setEditable(false);
        messageArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        messageArea.setLineWrap(true);
        messageArea.setWrapStyleWord(true);

        JScrollPane scrollPane = new JScrollPane(messageArea);
        scrollPane.setBorder(BorderFactory.createTitledBorder("消息记录"));
        add(scrollPane, BorderLayout.CENTER);

        // 输入面板
        JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

        inputField = new JTextField();
        inputField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        inputField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        sendButton = new JButton("发送");
        sendButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }


        });

        inputPanel.add(inputField, BorderLayout.CENTER);
        inputPanel.add(sendButton, BorderLayout.EAST);

        add(inputPanel, BorderLayout.SOUTH);

        // 添加示例消息
        addSampleMessages();
    }

    private void sendMessage() {
        //获取输入框内容
       
        String message = inputField.getText();
           
            inputField.setText("");//清空输入框

        //发送消息给服务器
        try {
            sendMsgToServer(message);
        } catch (Exception e) {
            e.printStackTrace();
        }


        if (!message.isEmpty()) {
            // 添加消息到消息区域

            // 滚动到最底部
            messageArea.setCaretPosition(messageArea.getDocument().getLength());
        }
    }

    private void sendMsgToServer(String message) throws  Exception  {
        //发送消息给服务器
        //1从socket中得到特殊数据输出流
        DataOutputStream dos= new DataOutputStream(socket.getOutputStream());
        dos.writeInt(2);
        dos.writeUTF(message);

    }

    private void addSampleMessages() {
        messageArea.append("系统提示: 欢迎使用内网通聊天系统!\n\n");
    }
}